package com.punch.course.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.utils.ImageUtil;
import com.punch.course.mapper.CourseImageMapper;
import com.punch.course.model.CourseImage;
import com.punch.course.service.CourseImageService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * CourseService-教室信息 服务类
 *
 * @author xiaol
 * @version 1.0.0
 * @since 2020/7/20
 */
@Service
public class CourseImageServiceImpl extends ServiceImpl<CourseImageMapper, CourseImage> implements CourseImageService {


    /**
     * 保存图片
     *
     * @param courseId  课程id
     * @param imageList 详情图片
     */
    @Override
    public void saveIntroduceImg(Long courseId, List<String> imageList) {
        List<CourseImage> insertList = new ArrayList<>();
        CourseImage courseImage;
        for (String img : imageList) {
            courseImage = new CourseImage();
            courseImage.setIntroduceImg(ImageUtil.removePrefix(img));
            courseImage.setCourseId(courseId);
            insertList.add(courseImage);
        }
        saveBatch(insertList);
    }

    /**
     * 修改图片
     *
     * @param courseId  课程id
     * @param imageList 详情图片
     */
    @Override
    public void updateIntroduceImg(Long courseId, List<String> imageList) {
        UpdateWrapper<CourseImage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseImage::getCourseId, courseId);
        remove(updateWrapper);
        if (CollUtil.isNotEmpty(imageList)) {
            saveIntroduceImg(courseId, imageList);
        }
    }
}





